package com.example.fw;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public abstract class HelperBase {

	protected ApplicationManager manager;
	protected WebDriver driver;
	private WebDriverWait wait;

	public HelperBase(ApplicationManager manager) {
		this.manager = manager;
		this.driver = manager.driver;
		wait = new WebDriverWait(driver, 30);
	}

	protected void type(By locator, String text) {
		WebElement element = wait.until(visibilityOfElementLocated(locator));
		if (text != null) {
			element.clear();
			element.sendKeys(text);
		}
	}

	protected void select(By locator, int index) {
		new Select(wait.until(visibilityOfElementLocated(locator))).selectByIndex(index);
	}

	
	protected String extractTextFromInput(By locator){
		return wait.until(visibilityOfElementLocated(locator)).getAttribute("value");
	}
	
	
	protected void click(By locator) {
		wait.until(visibilityOfElementLocated(locator)).click();
	}

	protected void selectByText(By locator, String text) {
		if (text != null) {
			new Select(wait.until(visibilityOfElementLocated(locator))).selectByVisibleText(text);
		}
	}

}
